package com.meditor.lib
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	
	public class BaseLoader extends EventDispatcher
	{
		public function BaseLoader()
		{
		}
		
		protected var _url:String;
		protected var _bytes:ByteArray;
		protected var _loader:Loader;
		protected var _name:String;
		protected var _type:String;
		
		/**
		 * 
		 * @param url 加载的外部资源
		 * @param lc
		 * 
		 */		
		public function loadResource(url:String,lc:LoaderContext = null):void
		{
			_url = url;
			_loader = new Loader;
			_loader.load(new URLRequest(url),lc);
			initEvent();
		}
		
		/**
		 * 
		 * @param bytes 加载外部字节文件
		 * @param lc
		 * 
		 */		
		public function loadBytes(bytes:ByteArray,lc:LoaderContext = null):void
		{
			_bytes = bytes;
			_loader = new Loader;
			_loader.loadBytes(bytes,lc);
			initEvent();
		}
		
		protected function initEvent():void
		{
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressFunction);
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeFunction);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorFunction);
		}
		
		protected function progressFunction(event:ProgressEvent):void
		{
			this.dispatchEvent(event);
		}
		
		protected function completeFunction(event:Event):void
		{
			this.dispatchEvent(event);
			destroyEvent();
		}
		
		protected function ioErrorFunction(event:IOErrorEvent):void
		{
			if( _url )
			{
				trace("读取文件错误!");
				trace("未找到文件"+_url);
			}
			if( _bytes )
			{
				trace("读取字节文件错误!");
				trace("未找到字节流"+_bytes);
			}
		}
		
		protected function destroyEvent():void
		{
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,progressFunction);
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeFunction);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorFunction);
		}
		
		public function destroyResource():void
		{
			_loader.unload();
			_loader = null;
			
		}
		
		/**
		 * 资源的url路径
		 */		
		public function get url():String
		{
			return _url;
		}

		public function set url(value:String):void
		{
			_url = value;
		}
		
		
		/**
		 * 资源的字节流
		 */	
		public function get bytes():ByteArray
		{
			return _bytes;
		}
		
		public function set bytes(value:ByteArray):void
		{
			_bytes = value;
		}

		
		/**
		 * 获取加载的对象
		 */	
		public function get loader():Loader
		{
			return _loader;
		}

		public function set loader(value:Loader):void
		{
			_loader = value;
		}
		
		
		/**
		 * 获取加载器的名称
		 */	
		public function get name():String
		{
			return _name;
		}
		
		
		/**
		 * 获取类型
		 */	
		public function get type():String
		{
			return _type;
		}
		
		/** 
		 * 清除加载的资源
		 */		
		public function clear():void
		{
			_loader.unload();
			_loader = null;
			BaseGC.gc();
		}
		
	}
}